/*
 * ProductionGenerator.java
 *
 * Created on July 14, 2006, 3:13 PM
 *
 */

package galronnlp.util;

import galronnlp.cfg.grammar.Rule;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author Daniel A. Galron
 */
public class ProductionGenerator {
    
    private Hashtable<Rule, Integer> prodTable;
    Integer nextInt = new Integer(-1);
    /** Creates a new instance of SymbolGenerator */
    public ProductionGenerator() {
        prodTable = new Hashtable<Rule, Integer>();
    }
    
    public Production create(Rule T) {
        if(prodTable.containsKey(T)) {
            return new Production(T, prodTable.get(T).intValue());
        } else {
            nextInt = new Integer(nextInt.intValue() + 1);
            prodTable.put(T, nextInt);
            return new Production(T, nextInt.intValue());
        }
    }
    
    public int size() {
        return prodTable.size();
    }
    
    public String toString() {
        String ret = "----------------------------\n";
        for(Enumeration<Rule> e = prodTable.keys(); e.hasMoreElements(); ) {
            Rule next = e.nextElement();
            ret += next + ": " + (Integer)prodTable.get(next) + "\n";
        }
        ret += "--------------------------------\n";
        return ret;
    }
    
}
